package activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.ci_edit.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import model.Constants;
import okhttp3.Call;
//
//import com.example.administrator.ci_edit.R;
//
//import javax.mail.MessagingException;

/**
 * Created by mary on 2017/7/13.
 */

public class ForgetPasswordActivity extends Activity {
    private EditText EmailAddress, vCode, psw, psw2;
    private Button getCode, submit;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private String username;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_forgetpass);

        sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();

        getCode = (Button) findViewById(R.id.getCode);
        submit = (Button) findViewById(R.id.submitBtn_new);
        EmailAddress = (EditText) findViewById(R.id.emailAddress);
        vCode = (EditText) findViewById(R.id.verificationCode);
        psw = (EditText) findViewById(R.id.newPsw);
        psw2 = (EditText) findViewById(R.id.newPsw2);

        getCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username = EmailAddress.getText().toString();
                if (null == username || username.length() <= 0) {
                    EmailAddress.requestFocus();
                    Toast.makeText(ForgetPasswordActivity.this, "邮箱不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    OkHttpUtils.post()
                            .url(Constants.SERVER_URL + "sendEmail")
                            .addParams("username", username)
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int id) {

                                }

                                @Override
                                public void onResponse(String response, int id) {
                                    if (response.equals("NOT_EXIST")) {
                                        Toast.makeText(ForgetPasswordActivity.this, "用户不存在", Toast.LENGTH_SHORT).show();
                                    } else if (response.equals("EMAIL_FAIL")) {
                                        Toast.makeText(ForgetPasswordActivity.this, "邮件发送失败，请稍后再试", Toast.LENGTH_SHORT).show();
                                    } else {
                                        Toast.makeText(ForgetPasswordActivity.this, "邮件已发送", Toast.LENGTH_SHORT).show();
                                        editor.putString("verificationCode", response);
                                    }
                                }
                            });
                }
            }
        });
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String true_code = sharedPreferences.getString("code", null);
                String code = vCode.getText().toString();
                String password1 = psw.getText().toString();
                String password2 = psw2.getText().toString();
                if (!true_code.equals(code)) {
                    Toast.makeText(ForgetPasswordActivity.this, "验证码不正确", Toast.LENGTH_SHORT).show();
                } else if (password1.length() <= 0 || null == password1) {
                    Toast.makeText(ForgetPasswordActivity.this, "请输入新密码", Toast.LENGTH_SHORT).show();
                } else if (!password1.equals(password2)) {
                    Toast.makeText(ForgetPasswordActivity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                } else {
                    username = EmailAddress.getText().toString();
                    OkHttpUtils.post()
                            .url(Constants.SERVER_URL + "sendEmail")
                            .addParams("username", username)
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int id) {

                                }

                                @Override
                                public void onResponse(String response, int id) {

                                }
                            });
                }
            }
        });
    }
//    Runnable run=new Runnable() {
//        @Override
//        public void run() {
//            try {
//                SendEmail sendEmail=new SendEmail(address);
//            } catch (MessagingException e) {
//                e.printStackTrace();
//            }
//        }
//    };
}
